package requrest

import (
	"errors"
	"go-project/dto"
	"go-project/models"
	"log"

	"github.com/gin-gonic/gin"
)

type TagSearchRequest struct {
	Pagination Pagination
	Name       string `form:"name"`
}

type TagInsertRequest struct {
	Name   string `json:"name" binding:"required,tag_name_unique" required_msg:"用户名不能为空" tag_name_unique_msg:"该标签名称已存在"`
	Weight int64  `json:"weight" binding:"max=100,min=0" max_msg:"权重最大不能大于100" min_msg:"权重最小不能小于0"`
	Color  string `json:"color"`
	dto.ValidateBindRequest
}

func (tagInsertReq *TagInsertRequest) BindAndValidate(c *gin.Context) error {
	if err := c.ShouldBindJSON(tagInsertReq); err != nil {
		c.Errors = append(c.Errors, c.Error(err).SetType(gin.ErrorTypeBind))
		c.Set("req_dto", tagInsertReq)
		return err
	}
	return nil
}
func (tagInsertReq *TagInsertRequest) Generate(data interface{}) *dto.ValidateBindRequest {
	tag, ok := data.(*models.Tag)
	if ok {
		log.Println(*tagInsertReq, "tag")
		tag.Name = tagInsertReq.Name
		tag.Weight = int8(tagInsertReq.Weight)
		tag.Color = tagInsertReq.Color
		return nil
	}
	tagInsertReq.Errors = errors.New("数据类型不正确，数据必须是models.Tag类型")
	return &tagInsertReq.ValidateBindRequest

}

type TagUpdateRequest struct {
	Id     uint   `json:"id"  binding:"required" required_msg:"id不能为空"`
	Name   string `json:"name" binding:"required,tag_name_unique=update" required_msg:"用户名不能为空" tag_name_unique_msg:"该标签名称已存在"`
	Weight int64  `json:"weight" binding:"max=100,min=0" max_msg:"权重最大不能大于100" min_msg:"权重最小不能小于0"`
	Color  string `json:"color"`
	dto.ValidateBindRequest
}

func (tagUpdateReq *TagUpdateRequest) BindAndValidate(c *gin.Context) error {
	if err := c.ShouldBindJSON(tagUpdateReq); err != nil {
		c.Errors = append(c.Errors, c.Error(err).SetType(gin.ErrorTypeBind))
		c.Set("req_dto", tagUpdateReq)
		return err
	}
	return nil
}
func (tagUpdateReq *TagUpdateRequest) Generate(data interface{}) *dto.ValidateBindRequest {
	tag, ok := data.(*models.Tag)
	if ok {
		tag.ID = tagUpdateReq.Id
		tag.Name = tagUpdateReq.Name
		tag.Weight = int8(tagUpdateReq.Weight)
		tag.Color = tagUpdateReq.Color
		return nil
	}
	tagUpdateReq.Errors = errors.New("数据类型不正确，数据必须是models.Tag")
	return &tagUpdateReq.ValidateBindRequest

}
